package com.lyf.service.impl;

import com.lyf.service.HystrixTestService;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/6
 * @VERSION 1.0
 * @DESC
 */
@Service
@DefaultProperties(defaultFallback = "globalFallback")// 还是在一个类中耦合？？,是否可以统一类管理？->通配》
public class HystrixTestServiceImpl implements HystrixTestService {

    //HystrixCommandProperties
    @Override
    public String paymentOk() {
        return "Handle successfully";
    }

    @Override
    @HystrixCommand(fallbackMethod = "fallbackMethod",commandProperties = {
         @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String paymentTimeOut() throws InterruptedException {
        int workTime = 2+ new Random().nextInt(3);
        //Thread.sleep(workTime); // 采用Thread 则不行》》？ main线程和子线程问题？
        TimeUnit.SECONDS.sleep(workTime);
        return "Time Out,cost + "+workTime+" sec";
    }

    public String fallbackMethod(){
        return "Sys is Busy!!";
    }

    public String globalFallback(){
        return "global...!";
    }
}
